package cn.kgc.springcloud.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * @author 课工场
 * @date 2024/4/27
 * @description   服务间如何通信
 */
@RestController
@RequestMapping("student")
public class StudentController {


    @Resource
    private DiscoveryClient discoveryClient;

    @Resource
    private LoadBalancerClient loadBalancerClient;


    @Resource
    private RestTemplate  rest;


    @GetMapping("info")
    public String  getInfo(){

        // 创建restTemplate 实例 发送http请求 实现服务间的通信    http://localhost:8552/score/info
        RestTemplate restTemplate = new RestTemplate();
        String result = restTemplate.getForObject(getHost()+"/score/info", String.class);
        return "student service ok  >>  invoke score service >>  "+result;
    }



    @GetMapping("info2")
    public String  getInfo2(){

        // 创建restTemplate 实例 发送http请求 实现服务间的通信    http://localhost:8552/score/info
        RestTemplate restTemplate = new RestTemplate();
        //从服务注册中心拉取可用通信节点
        List<ServiceInstance> score = discoveryClient.getInstances("SCORE");
        Random random = new Random();
        int i = random.nextInt(score.size());
        ServiceInstance serviceInstance = score.get(i);
        URI uri = serviceInstance.getUri();
        String result = restTemplate.getForObject(uri + "/score/info", String.class);
        return "student service ok  >>  invoke score service >> " +result;
    }


    @GetMapping("info3")
    public String  getInfo3(){

        // 创建restTemplate 实例 发送http请求 实现服务间的通信    http://localhost:8552/score/info
        RestTemplate restTemplate = new RestTemplate();
        //从服务注册中心拉取可用通信节点   默认负载均衡策略 轮循
        ServiceInstance serviceInstance = loadBalancerClient.choose("SCORE");
        URI uri = serviceInstance.getUri();
        String result = restTemplate.getForObject(uri + "/score/info", String.class);

        return "student service ok  >>  invoke score service >> "+result;
    }

    @GetMapping("info4")
    public String  getInfo4(){

        // 首先根据服务名称 从注册中心拉取可用节点，在通过retTemplate向目标接口发起http请求
        //  使用 服务名称发送请求  请求的地址是固定写法  存在路径耦合问题    http://SCORE/score/information
        String result = rest.getForObject("http://SCORE/score/info", String.class);
        return "student service ok  >>  invoke score service >> "+result;
    }


    //随机的负载均衡策略
    public String  getHost(){
        List<String> hosts = Arrays.asList("http://localhost:8552", "http://localhost:8553", "http://localhost:8554");
        Random random = new Random();
        int index = random.nextInt(hosts.size());
       return hosts.get(index);
    }


}
